package com.example.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.config.Utils.MedInfoInsertUtil;
import com.example.entity.MI_Table;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 文件上传
 *
 * @author: liu
 * 2022-03-19-14:58
 **/
@Api(tags = "FileUploadController")
@RestController
public class FileUploadController {
    //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private MedInfoInsertUtil medInfoInsertUtil;

    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public boolean upload(@RequestParam("file") MultipartFile file) throws IOException{
        if (file.isEmpty()) {
            System.out.println("文件未获取。。。");
            return false;
        }
        InputStream inputStream = file.getInputStream();

        EasyExcel.read(inputStream, MI_Table.class, new AnalysisEventListener<MI_Table>() {
            int num=1;
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("Excel全部读完被执行......");
            }

            @Override
            public void invoke(MI_Table data, AnalysisContext analysisContext) {

                medInfoInsertUtil.addMedInfo(data);

                System.out.println("解析num行: "+ num + "==" + data);
                num++;
            }

        }).sheet().doRead();


        return true;
    }



//    public Map<String ,Object> fileupload(MultipartFile file, HttpServletRequest request){
//        Map<String,Object> result = new HashMap<>();
//        String originalName = file.getOriginalFilename();
//        if(!originalName.endsWith(".xlsx")){
//            result.put("status","error");
//            result.put("msg","文件类型不对");
//            return result;
//        }
//        String format = sdf.format(new Date());
//        String realPath = request.getServletContext().getRealPath("/") + format;
//        File folder = new File(realPath);
//        if(!folder.exists()){
//            folder.mkdirs();
//        }
//        String newName = UUID.randomUUID().toString() + ".xlsx";
//        try {
//            file.transferTo(new File(folder,newName));
//            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
//                    + format + newName;
//            result.put("status","success");
//            result.put("url",url);
//        } catch (IOException e) {
//            result.put("status","error");
//            result.put("msg",e.getMessage());
//        }
//        return result;
//    }






















}
